package com.baor.utils;

import com.baor.domain.StuVo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtils {
    public static List<StuVo> excelToShopIdList(InputStream inputStream) throws IOException, InvalidFormatException {
        Workbook workbook = WorkbookFactory.create(inputStream);
        inputStream.close();
        Sheet sheet = workbook.getSheetAt(0);
        //获取Excel表里行数
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
        //创建集合储存行对象
        ArrayList<StuVo> detaileds = new ArrayList<>();
        //从第二行开始，遍历表格
        for (int i = 1; i < physicalNumberOfRows; i++) {
            //创建对象储存行数据
            StuVo detailed = new StuVo();
            //获取当前行数据
            Row row = sheet.getRow(i);
            //获取单元格的值存入对象中
            Cell cell_id=row.getCell(0);
            cell_id.setCellType(CellType.STRING);



//            detailed.setStuId(Integer.valueOf(cell_id.getStringCellValue()));

            DataFormatter dataFormatter = new DataFormatter();

            Cell cell1 = row.getCell(1);
            if (cell1 != null) {
                detailed.setStuName(dataFormatter.formatCellValue(cell1));
            }

            Cell cell2 = row.getCell(2);
            if (cell2 != null) {
                detailed.setStuSex(dataFormatter.formatCellValue(cell2));
            }

            Cell cell3 = row.getCell(3);
            if (cell3 != null) {
                detailed.setStuNumber(dataFormatter.formatCellValue(cell3));
            }

            Cell cell4 = row.getCell(4);
            if (cell4 != null) {
                detailed.setStuAge(Integer.parseInt(dataFormatter.formatCellValue(cell4)));
            }

            Cell cell5 = row.getCell(5);
            if (cell5 != null) {
                detailed.setClassId(Integer.parseInt(dataFormatter.formatCellValue(cell5)));
            }

//            Cell cell_radio=row.getCell(6);
//            cell_radio.setCellType(CellType.STRING);
//            detailed.setRatio(Double.valueOf(cell_radio.getStringCellValue()));
            //将对象放到集合中
            detaileds.add(detailed);
        }
        return detaileds;
    }
}
